home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 03 - 1987 / 03.07 Jul 87 / bit map source / dragsample2.p < prev    next >
Encoding:
Text File  |  1987-04-24  |  3.6 KB  |  138 lines  |  [TEXT/MPS ]

  1. program DragExample2;
  2. {$D+}    {put in debug names}
  3. {$R+}    {range checking on}
  4. {$OV+} {overflow checking on}
  5. {$N+}    {pass routine names to linker, so they're not anonymous}
  6. USES {$LOAD pinterfaces.dump}        
  7.         MemTypes,QuickDraw,OsIntf,PasLibIntf,ToolIntf,
  8.         PackIntf,IntEnv,CursorCtl,
  9.         {$LOAD}
  10.         {$U UDrag.p}    DragManager;
  11.  
  12. var
  13.     myPic : PicHandle;
  14.     myRect : Rect;
  15.     wPort: GrafPtr;
  16.     whichPict,
  17.     x,y,xinc,yinc : integer;
  18.     xyPt, lastXYPt : point;
  19.     myEventRecord : EventRecord;
  20.     lastCount : longint;
  21.     
  22.     myDragStuff : DragHandle;
  23.  
  24. procedure Debugger; { turned off for now
  25.     INLINE $A9FF;}
  26.     begin
  27.         {stub}
  28.     end;
  29.  
  30. procedure BounceItAround;
  31. begin
  32.     GetMouse(xyPt);
  33.     xinc := xyPt.h;
  34.     yinc := xyPt.v;
  35. {starting position}
  36.     y := (screenBits.bounds.right - screenBits.bounds.left) div 2;
  37.     x := (screenBits.bounds.bottom - screenBits.bounds.top) div 2;
  38. {changes don't occur until the focal point moves, so
  39.  this makes sure it's drawn the first time.}
  40.     SetPt(lastXYPt, -100, -100);
  41. repeat
  42.     GetMouse(xyPt);
  43. {use the cursor position to set the velocity of the
  44.  bouncing picture.  It doesn't change the direction,
  45.  just the speed.}
  46.     if xinc > 0 then xinc := xyPt.h else xinc := -xyPt.h;
  47.     if yinc > 0 then yinc := xyPt.v else yinc := -xyPt.v;
  48. {move the focal point}
  49.     x := x + xinc;
  50.     y := y + yinc;
  51. {keep it on the screen and bounce off the edges if needed}
  52.     with screenBits.bounds do
  53.     if y < top then 
  54.     begin y := top; yinc := -yinc; end
  55.     else if y > bottom then 
  56.         begin y := bottom; yinc := -yinc; end;
  57.  
  58.     with screenBits.bounds do
  59.     if x < left then
  60.     begin x := left; xinc := -xinc; end
  61.     else if  x > right then
  62.     begin x := right; xinc := -xinc; end;
  63.     
  64.     SetPt( xyPt, x, y );
  65.  
  66. {if the movement is far enough, the next line of code (commented out)
  67.  can be used to make the image persist long enough that it doesn't appear
  68.  ghostly.  On the other hand, if the movement is very small, the image
  69.  generally looks nice and solid and does not benefit from delays.}
  70.     {if TickCount >= lastCount + 2 then}
  71.     if not EqualPt( xyPt, lastXYPt) then 
  72.     begin
  73.     {if CapsLockKey, don't show the shadow, else show it}
  74.         if EventAvail(0, myEventRecord) then {twiddle thumbs};
  75.         if BTST(myEventRecord.modifiers, 10) 
  76.         then with shadowStuff do 
  77.             begin dx := 0; dy := 0;  visible := false; end 
  78.         else with shadowStuff do
  79.         begin
  80.             dx := xyPt.h div 8 - 32;
  81.             dy := xyPt.v div 8 - 20;
  82.             visible := true;
  83.         end;
  84.  
  85.         DragItTo( myDragStuff, xyPt, true );
  86.  
  87.         lastXYPt := xyPt;
  88.         lastCount := TickCount;
  89.     end;
  90.     {allow for FKEY access and give DAs and such time}
  91.     if GetNextEvent( keyDownMask, myEventRecord ) then {do nothing};
  92.     SystemTask;
  93. until button;
  94. repeat until not button;
  95. end; {bounceitaround}
  96.  
  97. begin{main}
  98.     InitGraf(@thePort);
  99.     
  100. {standalones need to init windows, but tools shouldn't}
  101.     if IEStandalone then InitWindows;
  102.     
  103.     GetWMgrPort( wPort );
  104.     SetPort( wPort );
  105.     ClipRect( screenBits.bounds );
  106.     InitCursor;    
  107.     
  108.     if not InitDrag( nil ) then exit( DragExample2 );
  109.     
  110. for whichPict := 1 to CountResources('PICT') do
  111. begin
  112.     myPic := PicHandle(GetIndResource('PICT', whichPict));
  113.     if myPic = nil then 
  114.         begin SysBeep(1); exit(DragExample2); end;
  115.     if not NewDraggable( myPic, nil, nil, myDragStuff )
  116.         then exit(DragExample2);
  117.     BounceItAround;    
  118.     DisposeDraggable( myDragStuff );
  119. {    Use this code to take a look at pictures that
  120.     display some behavior you don't understand.}
  121. {    repeat until not button;
  122.     repeat until button;
  123.     FillRect( screenBits.bounds, white );
  124.     HLock( Handle( myPic ));
  125.         DrawPicture( myPic, myPic^^.picFrame );
  126.         myRect := myPic^^.picFrame;
  127.         with myRect do
  128.             OffsetRect( myRect, -left, -top );
  129.         DrawPicture( myPic, myRect );
  130.     HUnlock( Handle( myPic ));
  131. }
  132.     ReleaseResource( Handle( myPic ));
  133. end;
  134.  
  135. CloseDrag( true );
  136.  
  137. end.
  138.